package org.hd.userlist.service.impl;

import org.hd.userlist.dao.UserListDao;
import org.hd.userlist.service.UserListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserListServiceImpl implements UserListService {


    @Resource
    UserListDao userListDao;

    @Override
    public List<Map<String, Object>> getUSerList() {

        List<Map<String, String>> result = userListDao.getUserList();

        if(result == null){
            return null;
        }

        List<Map<String,Object>> list = new ArrayList<>();

        String name = "";
        for(int i=0;i<result.size();i++){
            if(!name.equals(result.get(i).get("username"))){
                name = result.get(i).get("username");
                HashMap<String,Object> map = new HashMap<>();
                map.put("username",result.get(i).get("username"));
                map.put("role",result.get(i).get("description"));
                map.put("status",result.get(i).get("status"));
                ArrayList<String> menus = new ArrayList<>();
                map.put("menus",menus);
                list.add(map);
            }
        }

        for(int i=0;i<list.size();i++){

            ArrayList<String> arr = null;

            for(int j=0;j<result.size();j++){
                if(list.get(i).get("username").equals(result.get(j).get("username"))){
                    arr = (ArrayList<String>)list.get(i).get("menus");
                    arr.add(result.get(j).get("pdescription")+" ");
                }
            }
        }

        return list;
    }

    /**
     *
     * @param flag
     * @param username
     * @return
     */
    @Override
    public boolean changeUserStatus(String username,boolean flag) {
        if(flag){
            return userListDao.changeUserStatus(username,1);
        }else {
            return userListDao.changeUserStatus(username, 0);
        }
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @Override
    public List<Map<String, String>> getRoleList() {
        return userListDao.getRoleList();
    }

    /**
     * 修改用户角色
     *
     * @param username
     * @param roleName
     * @return
     */
    @Override
    public boolean changeUserRole(String username, String roleName) {
        return userListDao.changeUserRole(username,roleName);
    }

    /**
     * 插入新用户
     *
     * @param username
     * @param password
     * @param role
     * @return 1:插入成功   2:插入失败   3:有重复用户
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertUser(String username, String password, String role) {

        String str = String.valueOf(System.currentTimeMillis());
        str +=  (int)((Math.random() * 9 + 1) * 10000);

        if(userListDao.checkUser(username) != null){
            return 3;
        }else{
            if(userListDao.insertUser(str,username, password)){
                if(userListDao.insertRole(username,role)){
                    return 1;
                }
            }else{
                return 2;
            }
        }
        return 2;
    }

    /***
     * 删除用户
     * @param username
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUser(String username) {

        if(userListDao.deleteUserRole(username) == 1){
            if(userListDao.deleteUserName(username) == 1){
                return true;
            }
        }
        return false;
    }
}
